package com.hrt.manage.sms.modual.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.SMSModual;
import com.hrt.manage.sms.modual.dao.SMSModualDao;


@Repository
public class SMSModualDaoImpl  extends BaseDaoImpl<SMSModual> implements SMSModualDao {

	@Override
	public List<SMSModual> listModuals() {
		// TODO Auto-generated method stub
		String hql="select sms from SMSModual sms where sms.isdelete=0";
		List<SMSModual> list=new ArrayList<SMSModual>();
		list=this.getHibernateTemplate().find(hql);
		return list;
	}

	
	/**
	 * 
	 */
	@Override
	public List<SMSModual> queryByPage(List<Map<String, Object>> conditions,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		String hql="select sms from SMSModual sms where sms.isdelete=0";
		List<SMSModual> list=new ArrayList<SMSModual>();
		if(conditions!=null && !conditions.isEmpty()){
			for(Map map:conditions){
				String fieldName=map.get("fieldName").toString();
				String fieldValue=map.get("fieldValue").toString();
				hql=hql+" and c."+fieldName+fieldValue+"";
			}
		}
		list=this.getHibernateTemplate().find(hql);
		return list;
	
	}


	@Override
	public SMSModual getModualById(Long moId) {
		
		final String hql="select sms from SMSModual sms where sms.isdelete=0 and sms.id="+moId+"";
		
		return this.getHibernateTemplate().execute(new HibernateCallback<SMSModual>(){

			@Override
			public SMSModual doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery(hql);
				return (SMSModual) query.uniqueResult();
			}
			
		});
	
	}


	@Override
	public SMSModual getModualByBusinessCode(Integer businessCode) {
		
		final String hql="select sms from SMSModual sms where sms.isdelete=0 and sms.bussinessCode="+businessCode+"";
		
		return this.getHibernateTemplate().execute(new HibernateCallback<SMSModual>(){

			@Override
			public SMSModual doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery(hql);
				return (SMSModual) query.uniqueResult();
			}
			
		});
	
	}

	

	

	
}
